package com.heima.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.repository.ApAssociateWordsRepository;
import com.heima.search.service.IApAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/4/28 2:13
 * @Version 1.0
 */

@Service
public class ApAssociateWordsServiceImpl implements IApAssociateWordsService {

    @Autowired
    private ApAssociateWordsRepository repository;

    @Override
    public ResponseResult search(ApArticleSearchDto dto) {

        // 判断用户输入的关键字为空,不需要查询
        if (ObjectUtil.isEmpty(dto) || StrUtil.isEmpty(dto.getSearchWords()) || StrUtil.isEmpty(dto.getSearchWords().trim())) {
            //返回空的集合
            ArrayList<ApAssociateWords> list = new ArrayList<>();
            return ResponseResult.okResult(list);
        }

        // 根据用户传入的关键字在联想词表中进行查询
        List<ApAssociateWords> list = repository.findTop5ByAssociateWordsContainsOrderByCreatedTimeDesc(dto.getSearchWords());

        return ResponseResult.okResult(list);
    }
}
